<template>
  <div class="container">
    <div class="title">
      <span>信息管理</span>
      <span class="back" @click="back" v-if="lin_user_id === 1">
          <i class="iconfont icon-fanhui"></i> 返回
      </span>
    </div>
    <el-divider></el-divider>
    <el-form label-width="160px">
      <div class="wrap">
        <el-form-item label="商家名称" prop="title">
          <el-input
              size="medium"
              v-model="title"
              placeholder="请填写商家名称"
          ></el-input>
        </el-form-item>
        <el-form-item label="商家状态" prop="title">
          <span v-if="status === '0'">不展示</span>
          <span v-if="status === '1'">展示</span>
        </el-form-item>
        <el-form-item label="">
          <upload-imgs
              ref="uploadEle"
              :value="imgValue"
              :rules="rules"
              :multiple="true"
              :sortable="true"
              :min-num="1"
              :max-num="5"
          />
        </el-form-item>
        <el-form-item label="商家所在地址" prop="address">
          <el-input
              size="medium"
              v-model="address"
              placeholder="请填写商家所在地址"
          ></el-input>
        </el-form-item>
        <el-form-item label="商家地址所对应坐标" prop="point">
          <el-input
              size="medium"
              v-model="point"
              placeholder="请填写商家地址所对应坐标"
          ></el-input>
          <a href="https://lbs.qq.com/tool/getpoint/" target="_blank">腾讯坐标拾取网址</a>
        </el-form-item>
        <el-form-item label="商家介绍">
          <div class="lin-wrap">
            <tinymce :defaultContent="content" @change="change" />
          </div>
        </el-form-item>
        <el-form-item class="submit">
          <el-button type="primary" @click="updateData()">提 交</el-button>
        </el-form-item>
      </div>
    </el-form>
  </div>
</template>

<script>
import SjMsg from '@/models/sj-msg'
import UploadImgs from '@/components/base/upload-imgs'
import Tinymce from '@/components/base/tinymce'
import User from '@/lin/models/user'

export default {
  components: {
    UploadImgs,
    Tinymce,
  },
  props: {
    SjMsgId: {
      type: Number,
    },
  },
  data() {
    return {
      lin_user_id: '',
      imgValue: [],
      title: '',
      status: '0',
      address: '',
      point: '',
      content: '',
      rules: {
        maxSize: 0.4,
      },
    }
  },
  mounted() {
    this.getAdminUser()
    this.getInfo()
  },
  methods: {
    async getAdminUser() {
      const res = await User.getInformation()
      this.lin_user_id = res.linUserId
    },
    async getInfo() {
      const info = await SjMsg.getDataInfo(this.SjMsgId)
      info.imgs.forEach((item) => {
        this.imgValue.push({
          delete: false,
          id: item.img.id,
          imgId: item.img_id,
          display: item.img.url,
        })
      })
      this.title = info.title
      this.status = info.status
      this.address = info.address
      this.point = info.point
      this.content = info.content
    },
    back() {
      this.$emit('editClose')
    },
    async updateData() {
      const self = this
      const imgValue = await this.$refs.uploadEle.getValue()
      const img_ids = []
      imgValue.forEach((item) => {
        img_ids.push({ imgId: item.imgId })
      })
      const info = {
        id: self.SjMsgId,
        img_ids,
        title: self.title,
        order_sort: self.order_sort,
        address: self.address,
        point: self.point,
        content: self.content,
      }
      const res = await SjMsg.updateData(info)
      if (res.error_code === 0) {
        self.imgValue = []
        this.$message.success(`${res.msg}`)
        if (this.lin_user_id === 1) {
          this.back()
        }
      }
    },
    change(val) {
      this.content = val
    },
  },
}
</script>

<style lang="scss" scoped>
.el-divider--horizontal {
  margin: 0;
}

.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 16px;
    font-weight: 500;
    text-indent: 40px;

    .back {
      float: right;
      margin-right: 40px;
      cursor: pointer;
    }
  }

  .wrap {
    padding: 20px;
  }

  .submit {
    float: left;
  }
}
</style>
